package com.hc.sort.call;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.*;
import javax.swing.border.*;

import com.hc.sort.server.pojo.BusinessWindow;
import com.hc.sort.server.pojo.Caller;
import com.hc.sort.server.pojo.Ticket;
import com.hc.sort.utils.NetClient;

public class CallUI extends JFrame {
	BusinessWindow busWin;
	Caller caller;
	Ticket currentTicket;

	private JPanel contentPane;
	JButton btnNextNo;
	JButton btnReCall;
	JButton btnSuccess;
	JButton btnFail;
	JLabel lblCurrentTicketNo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CallUI frame = new CallUI(null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CallUI(BusinessWindow busWin,Caller caller) {
		this.busWin=busWin;
		this.caller=caller;
		
		setTitle("叫号系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 538, 626);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCurrent = new JLabel("当前");
		lblCurrent.setFont(new Font("宋体", Font.PLAIN, 18));
		lblCurrent.setBounds(247, 21, 45, 38);
		contentPane.add(lblCurrent);
		
		lblCurrentTicketNo = new JLabel("A1005");
		lblCurrentTicketNo.setFont(new Font("宋体", Font.PLAIN, 20));
		lblCurrentTicketNo.setBounds(239, 69, 64, 38);
		contentPane.add(lblCurrentTicketNo);
		
		JLabel lblWaiting = new JLabel("等待人数");
		lblWaiting.setFont(new Font("宋体", Font.PLAIN, 20));
		lblWaiting.setBounds(223, 117, 80, 38);
		contentPane.add(lblWaiting);
		
		JLabel lblCount = new JLabel("15");
		lblCount.setFont(new Font("宋体", Font.PLAIN, 20));
		lblCount.setBounds(248, 165, 44, 38);
		contentPane.add(lblCount);
		
		btnNextNo = new JButton("下一个号");
		btnNextNo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//下一个号按钮设置成disable
				//按钮：如果拿不到号或者处理完成，重新设置为enable，否则保持为disable，直到处理完成
				btnNextNo.setEnabled(false);
				
				//叫号：调用服务器端，获取今天+业务类型匹配+还没有叫号+最早的那张票
				HashMap reqMap = new HashMap();
				reqMap.put("command", "jiaohao");
				reqMap.put("arg0", busWin);
				reqMap.put("arg1", caller);
				
				//发送数据给服务器，并接收返回数据
				HashMap resultMap=(HashMap) NetClient.sendMsg(reqMap);
				int result=(int) resultMap.get("result");
				if (result>=0) {
					currentTicket=(Ticket) resultMap.get("arg0");
					//叫到号，显示在界面上
					String no=currentTicket.getBusinessCode()+currentTicket.getNo();
					lblCurrentTicketNo.setText(no);
					//设置按钮可以点击
					btnReCall.setEnabled(true);
					btnSuccess.setEnabled(true);
					btnFail.setEnabled(true);
				}else {
					JOptionPane.showMessageDialog(null, "没有号需要处理！");
					
					//按钮恢复为可以点击
					btnNextNo.setEnabled(true);
				}
			}
		});
		btnNextNo.setFont(new Font("宋体", Font.PLAIN, 16));
		btnNextNo.setBounds(191, 245, 134, 38);
		contentPane.add(btnNextNo);
		
		btnReCall = new JButton("重叫");
		btnReCall.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				HashMap reqMap = new HashMap();
				reqMap.put("command", "chongjiao");
				reqMap.put("arg0", currentTicket.getId());
				
				//发送数据
				HashMap retMap=(HashMap) NetClient.sendMsg(reqMap);
			}
		});
		btnReCall.setFont(new Font("宋体", Font.PLAIN, 16));
		btnReCall.setBounds(191, 323, 134, 38);
		contentPane.add(btnReCall);
		
		btnSuccess = new JButton("办理成功");
		btnSuccess.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				HashMap reqMap = new HashMap();
				reqMap.put("command", "handlingSuccess");
				reqMap.put("arg0", currentTicket.getId());
				reqMap.put("arg1", 1);
				
				//发送数据
				HashMap retMap=(HashMap) NetClient.sendMsg(reqMap);
				JOptionPane.showMessageDialog(null, "业务办理成功！");
				init();
			}
		});
		btnSuccess.setFont(new Font("宋体", Font.PLAIN, 16));
		btnSuccess.setBounds(191, 402, 134, 38);
		contentPane.add(btnSuccess);
		
		btnFail = new JButton("办理失败");
		btnFail.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				HashMap reqMap = new HashMap();
				reqMap.put("command", "handlingFailure");
				reqMap.put("arg0", currentTicket.getId());
				reqMap.put("arg1",0);
				
				//发送数据
				HashMap retMap=(HashMap) NetClient.sendMsg(reqMap);
				JOptionPane.showMessageDialog(null, "业务办理失败！");
				init();
			}
		});
		btnFail.setFont(new Font("宋体", Font.PLAIN, 16));
		btnFail.setBounds(191, 481, 134, 38);
		contentPane.add(btnFail);
		
		btnReCall.setEnabled(false);
		btnSuccess.setEnabled(false);
		btnFail.setEnabled(false);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon("D:\\course\\eclipse-workspace\\HuicaiBankSortSys\\src\\img\\叫号背景.jpg"));
		lblNewLabel_1.setBounds(0, 0, 524, 579);
		contentPane.add(lblNewLabel_1);
	}
	
	void init() {
		btnNextNo.setEnabled(true);
		btnReCall.setEnabled(false);
		btnSuccess.setEnabled(false);
		btnFail.setEnabled(false);
	}
}
